import plotly.offline as pyo
from plotly.graph_objs import *
import chart_studio.plotly as py
import pandas as pd
from pandas import DataFrame
pyo.offline.init_notebook_mode()
healthcare = pd.read_csv(r"../Data/lifeExpectancy-HealthcareSpending.csv", index_col = 0)
healthcare.head()
| Country | Life expectancy | Spending per capita($) | |
|---|---|---|---|
| 0 | United States | 78.94 | 9024 |
| 1 | Switzerland | 82.85 | 6786 |
| 2 | Norway | 81.75 | 6081 |
| 3 | Netherlands | 81.30 | 5276 |
| 4 | Germany | 80.84 | 5119 |
pyo.iplot([{'type' : 'scatter',
'mode' : 'markers',
'x' : healthcare['Spending per capita($)'],
'y' : healthcare['Life expectancy'],
'text' : healthcare['Country'],}])
healthcare['hovertext'] = healthcare.apply(lambda x:
"<b>{}</b><br>Life expectancy: {:.0f} years<br>Healthcare spend per capita: ${:,}".format(x['Country'],
x['Life expectancy'],
x['Spending per capita($)']),
axis = 1)
healthcare.head()
| Country | Life expectancy | Spending per capita($) | hovertext | |
|---|---|---|---|---|
| 0 | United States | 78.94 | 9024 | <b>United States</b><br>Life expectancy: 79 ye... |
| 1 | Switzerland | 82.85 | 6786 | <b>Switzerland</b><br>Life expectancy: 83 year... |
| 2 | Norway | 81.75 | 6081 | <b>Norway</b><br>Life expectancy: 82 years<br>... |
| 3 | Netherlands | 81.30 | 5276 | <b>Netherlands</b><br>Life expectancy: 81 year... |
| 4 | Germany | 80.84 | 5119 | <b>Germany</b><br>Life expectancy: 81 years<br... |
healthcare.loc[0,'hovertext']
'<b>United States</b><br>Life expectancy: 79 years<br>Healthcare spend per capita: $9,024'
def addTextNote(row):
if row['Life expectancy'] > 70 and row['Spending per capita($)'] < 6000:
return ''
else:
return " " + row['Country']
healthcare['textnote'] = healthcare.apply(addTextNote, axis = 1)
healthcare.head()
| Country | Life expectancy | Spending per capita($) | hovertext | textnote | |
|---|---|---|---|---|---|
| 0 | United States | 78.94 | 9024 | <b>United States</b><br>Life expectancy: 79 ye... | United States |
| 1 | Switzerland | 82.85 | 6786 | <b>Switzerland</b><br>Life expectancy: 83 year... | Switzerland |
| 2 | Norway | 81.75 | 6081 | <b>Norway</b><br>Life expectancy: 82 years<br>... | Norway |
| 3 | Netherlands | 81.30 | 5276 | <b>Netherlands</b><br>Life expectancy: 81 year... | |
| 4 | Germany | 80.84 | 5119 | <b>Germany</b><br>Life expectancy: 81 years<br... |
healthcareTrace = {'type' : 'scatter',
'mode' : 'markers',
'text' : healthcare['hovertext'],
'x' : healthcare['Spending per capita($)'],
'y' : healthcare['Life expectancy'],
'hoverinfo' : 'text',
'showlegend' : False,
'marker' : {'color' : '#B22222'}}
data = Data([healthcareTrace])
layout = {'title' : 'Healthcare Spending and Life Expectancy',
'hovermode' : 'closest',
'xaxis' : {'title' : 'Healthcare spending per capita',
'tickformat' : '$,',
'range' : [0, healthcare['Spending per capita($)'].max()*1.05]},
'yaxis' : {'title' : 'Life expectancy (years)',
'range' : [healthcare['Life expectancy'].min()*0.95,
healthcare['Life expectancy'].max()*1.05]}}
fig = Figure(data=data, layout=layout)
pyo.iplot(fig)
textNote = [{'type' : 'scatter',
'mode' : 'text',
'textposition' : 'middle right',
'text' : healthcare['textnote'],
'x' : healthcare['Spending per capita($)'],
'y' : healthcare['Life expectancy'],
'hoverinfo' : 'none',
'showlegend' : False}]
dataUpdated = Data([healthcareTrace]) + textNote
fig = Figure(data=dataUpdated, layout=layout)
pyo.iplot(fig)
fig['layout']['xaxis'].update({'range' : [0, healthcare['Spending per capita($)'].max() * 1.15]})
pyo.iplot(fig)